package org.opens.converter.sqlType;

import org.opens.converter.AbstractConverterFactory;
import org.opens.converter.constant.ConverterConfig;
import org.opens.converter.mybatisJdbcType.I_MybatisJdbcTypeConverter;
import org.opens.converter.sqlType.handle.MysqlSqlTypeConverterHandle;
import org.opens.converter.sqlType.handle.OracleSqlTypeConverterHandle;
import org.opens.db.constant.DbType;
import org.opens.exception.BusinessException;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2023/3/7
 */
public class SqlTypeConverterFactory extends AbstractConverterFactory {

    private static Map<DbType, I_SqlTypeConverter> sqlTypeConverterMap = new HashMap<>(ConverterConfig.defaultConverterMapSize);

    static {
        sqlTypeConverterMap.put(DbType.MYSQL, new MysqlSqlTypeConverterHandle());
        sqlTypeConverterMap.put(DbType.ORACLE, new OracleSqlTypeConverterHandle());
    }

    public static I_SqlTypeConverter getInstance(DbType dbType) {
        I_SqlTypeConverter sqlTypeConverter = sqlTypeConverterMap.get(dbType);
        if (sqlTypeConverter == null) {
            throw new BusinessException("根据数据库类型" + dbType.getTypeName() + "未获取到有效的类型映射数据, 请先增加转换规则!");
        }
        return sqlTypeConverter;
    }

    @Override
    public I_SqlTypeConverter getSqlTypeConverter(DbType dbType) {
        return SqlTypeConverterFactory.getInstance(dbType);
    }

    @Override
    public I_MybatisJdbcTypeConverter getMybatisJdbcTypeConverter(DbType dbType) {
        throw new RuntimeException("no support");
    }
}
